<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 1、导入 ol 依赖 -->
    <link rel="stylesheet" href="https://lib.baomitu.com/ol3/4.6.5/ol.css" />
    <script src="https://lib.baomitu.com/ol3/4.6.5/ol.js"></script>
  </head>
  <body>
    <!-- 2、设置地图挂载点（容器） -->
    <div id="map" style="width: 100%; height: 100%"></div>
    <!-- 3、初始化高德图层 -->
    <script>
      // 3、初始化高德图层
      const gaode = new ol.layer.Tile({
        title: "高德地图",
        source: new ol.source.XYZ({
          url: "http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}",
          wrapX: false,
        }),
      });
      // 4、初始化 openlayer 地图
      const map = new ol.Map({
        target: "map", // 将初始化的地图挂在到 id 为 map 的 dom 元素上
        layers: [gaode], // 设置图层
        // view：设置视图
        view: new ol.View({
          center: [114.3, 30.5], // 地图中心点
          zoom: 10, // 地图缩放级别
          projection: "EPSG:4326", // 地图投影
          // maxZoom: 18,
        }),
      });

      // 创建一个点要素
      const point = new ol.Feature({
        geometry: new ol.geom.Point([114.3, 30.5]),
      });
      // 创建点要素的样式
      const pointStyle = new ol.style.Style({
        image: new ol.style.Circle({
          radius: 10,
          fill: new ol.style.Fill({
            color: "#ff2d51",
          }),
        }),
      });
      // 给点要素设置样式
      point.setStyle(pointStyle);
      // 创建线要素
      const line = new ol.Feature({
        geometry: new ol.geom.LineString([
          [114.3, 33.5],
          [116.3, 32.5],
        ]),
      });
      // 创建线要素的样式
      const lineStyle = new ol.style.Style({
        stroke: new ol.style.Stroke({
          color: "#ff2d51",
          width: 2,
        }),
      });
      line.setStyle(lineStyle);
      // 创建面要素
      const polygon = new ol.Feature({
        geometry: new ol.geom.Polygon([
          [
            [114.3, 30.5],
            [116.3, 30.5],
            [116.3, 30.6],
            [114.3, 30.7],
          ],
        ]),
      });
      const polygonStyle = new ol.style.Style({
        stroke: new ol.style.Stroke({
          color: "#ff2d51",
          width: 2,
        }),
        fill: new ol.style.Fill({
          color: "rgba(255, 0, 0, 0.1)",
        }),
      });
      polygon.setStyle(polygonStyle);
      // 创建矢量数据源
      const source = new ol.source.Vector({
        features: [point, line, polygon],
      });
      // 创建矢量图层
      const layer = new ol.layer.Vector({
        source,
      });
      map.addLayer(layer);
    </script>
  </body>
</html>
